## All functions needed to run BG models -----

## OLS models ------
run.OLS.model.agency <- function(outcome,data){
  outcome <- enexpr(outcome)
  form <- as.formula(paste(quo_text(outcome),'~log(1+sumallLESOaidBG_lag)+percentinpoverty+
  logmedianhouseholdincome+unemployment+logpop+sharemales+percblack+share1519+share2024+
  share2534|g+state_year|0|State'))
  data %>% felm(form, data=.)
}

run.OLS.model.county <- function(outcome,data){
  outcome <- enexpr(outcome)
  form <- as.formula(paste(quo_text(outcome),'~log(1+sumallLESOaidBG_lag)+percentinpoverty+logmedianhouseholdincome+
                           unemployment+
                           logpop+sharemales+percblack+share1519+share2024+share2534|
                           g+state_year|0|State'))
  data %>% felm(form, data=.)
}

run.ols.model <- function(outcome,data){
  outcome <- enexpr(outcome)
  statetimefe_cols <- grep("^statetimefe_",names(data))
  form <-  as.formula(paste(quo_text(outcome),' ~ PovertyPercentAllAges + lMedian + Unempl_Rate +
                            lpop +ltotal_cost+ sharemale + shareblack +shareage1519+
                            shareage2024 + shareage2529 + shareage3034',
                            '| fips+',paste(colnames(data)[statetimefe_cols], collapse='+'),'|0',
                            '|State'))
  data %>% felm(form,data=.)
}

## 2SLS models
run.IV.model.agency <- function(outcome,data){
  outcome <- enexpr(outcome)
  form <-  as.formula(paste(quo_text(outcome),
                            '~percentinpoverty+logmedianhouseholdincome+unemployment+logpop+sharemales+
                            percblack+share1519+share2024+share2534|
                            g+state_year|(log(1+sumallLESOaidBG_lag)~milex_iv)|State'))
  data %>% felm(form,data=.)
}

run.IV.model.county <- function(outcome,data){
  outcome <- enexpr(outcome)
  form <-  as.formula(paste(quo_text(outcome),
                            '~percentinpoverty+logmedianhouseholdincome+
                            unemployment+
                            logpop+sharemales+percblack+share1519+share2024+share2534|
                            g+state_year|(log(1+sumallLESOaidBG_lag)~milex_iv)|State'))
  data %>% felm(form,data=.)
}

run.IV.model.BG <- function(outcome,data){
  outcome <- enexpr(outcome)
  statetimefe_cols <- grep("^statetimefe_",names(data))
  form <-  as.formula(paste(quo_text(outcome),
                            '~ PovertyPercentAllAges + lMedian + Unempl_Rate + 
                            lpop +sharemale + shareblack +shareage1519+
                            shareage2024 + shareage2529 + shareage3034', 
                            '| fips+',paste(colnames(data)[statetimefe_cols], collapse='+'),
                            '|(ltotal_cost~milex_iv)',
                            '|State'))
  data %>% felm(form,data=.)
}

## Reduced Form models -------
reduced.agency <- function(outcome,data){
  outcome <- enexpr(outcome)
  form <-  as.formula(paste(quo_text(outcome),
                            '~milex_iv+percentinpoverty+logmedianhouseholdincome+unemployment+logpop+sharemales+
                            percblack+share1519+share2024+share2534|
                            g+state_year|0|State'))
  data %>% felm(form,data=.)
}

reduced.county <- function(outcome,data){
  outcome <- enexpr(outcome)
  form <-  as.formula(paste(quo_text(outcome),
                            '~milex_iv + percentinpoverty+logmedianhouseholdincome+
                            unemployment+
                            logpop+sharemales+percblack+share1519+share2024+share2534|
                            g+state_year|0|State'))
  data %>% felm(form,data=.)
}

reduced.BG <- function(outcome,data){
  outcome <- enexpr(outcome)
  statetimefe_cols <- grep("^statetimefe_",names(data))
  form <-  as.formula(paste(quo_text(outcome),
                            '~ milex_iv + PovertyPercentAllAges + lMedian + Unempl_Rate + 
                            lpop +sharemale + shareblack +shareage1519+
                            shareage2024 + shareage2529 + shareage3034', 
                            '| fips+',paste(colnames(data)[statetimefe_cols], collapse='+'),'|0',
                            '|State'))
  data %>% felm(form,data=.)
}
